package com.xinnet.mms.config.service.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.mms.config.dao.FlowTurnConfigDao;
import com.xinnet.mms.config.service.FlowTurnConfigService;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.entity.PublicConfig;
import com.xinnet.mms.utils.CustomerManage;

@Service("flowTurnConfigService")
public class FlowTurnConfigServiceImpl implements FlowTurnConfigService {

	@Autowired
	private FlowTurnConfigDao flowTurnConfigDao;
	
	/**取得产品设置信息集合*/
	@Override
	public List<Product> getProductConfig() throws SQLException {
		return flowTurnConfigDao.getProductConfig();
	}

	/**批量修改产品流转规则*/
	@Override
	public void updateFlowConfig(String ids,
			String sysallocationNodealholdtime, String pushNodealholdtime,
			String redevNodealholdtime,
			String startServiceCycle, String endServiceCycle,
			String goOpenseaTime) throws SQLException {
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//分割id集
		String[] id=ids.split(":");
		for(int i=0;i<id.length;i++) {
			//添加产品历史
			this.addProductHistoryFromConfig(id[i]);
			//封装数据
			Product product=new Product();
			product.setId(id[i]);
			product.setSysallocationNodealholdtime(Integer.parseInt(sysallocationNodealholdtime));
			product.setPushNodealholdtime(Integer.parseInt(pushNodealholdtime));
			product.setRedevNodealholdtime(Integer.parseInt(redevNodealholdtime));
			product.setStartServiceCycle(Integer.parseInt(startServiceCycle));
			product.setEndServiceCycle(Integer.parseInt(endServiceCycle));
			product.setGoOpenseaTime(Integer.parseInt(goOpenseaTime));
			product.setAuditEffect(CustomerManage.USE_NO);
			//product.setEffectDate(sf.format(new Date()));
			product.setUpdateDate(sf.format(new Date()));
			//调用update设置流转规则
			flowTurnConfigDao.updateflowConfig(product);
		}
	}

	/**增加产品历史*/
	@Override
	public void addProductHistoryFromConfig(String id) throws SQLException {
		flowTurnConfigDao.addProductHistoryFromConfig(flowTurnConfigDao.getProductById(id));
	}

	/**修改所有产品线的二次开发停顿期*/
	@Override
	public void updateMiddleServiceCycle(int MiddleServiceCycle) throws SQLException {
		flowTurnConfigDao.updateMiddleServiceCycle(MiddleServiceCycle);
	}

	/**查询产品优先级列表*/
	@Override
	public List<Product> getProductPriority() throws SQLException {
		return flowTurnConfigDao.getProductPriority();
	}

	/**取得产品设置信息集合 (审核状态)*/
	@Override
	public List<Product> getAuditProductConfig() throws SQLException {
		return flowTurnConfigDao.getAuditProductConfig();
	}

	/**设置产品规则为审核通过*/
	@Override
	public void updateProductAuditYes(String id) throws SQLException {
		flowTurnConfigDao.updateProductAuditYes(id);
	}

	/**设置产品规则为审核不通过*/
	@Override
	public void updateProductAuditNo(String id) throws SQLException {
		flowTurnConfigDao.updateProductAuditNo(id);
	}

	/**修改二次开发停顿期 加入审批*/
	@Override
	public void updateMiddleServiceCycleAddCheck(int day) throws SQLException {
		flowTurnConfigDao.updateMiddleServiceCycleAddCheck(day);
	}

	/**查询二次开发停顿期*/
	@Override
	public PublicConfig getMiddleServiceCycleInCheck() throws SQLException {
		return flowTurnConfigDao.getMiddleServiceCycleInCheck();
	}

	/**审批二次开发停留期为拒绝*/
	@Override
	public void updateMiddleServiceCycleAuditNo() throws SQLException {
		flowTurnConfigDao.updateMiddleServiceCycleAuditNo();
	}

	/**取得业绩保留期*/
	@Override
	public PublicConfig getPerfDayInCheck() throws SQLException {
		return flowTurnConfigDao.getPerfDayInCheck();
	}
	
	/**取得到期预警时间*/
	@Override
	public PublicConfig getWarningTime() throws SQLException {
		return flowTurnConfigDao.getWarningTime();
	}

	/**修改业绩保留期加入审核*/
	@Override
	public void updatePerfDayInCheck(int day) throws SQLException {
		flowTurnConfigDao.updatePerfDayInCheck(day);
	}

	/**确认设置优先级*/
	@Override
	public void updatePriority(String id,int priority) throws SQLException {
		Product product=new Product();
		product.setId(id);
		product.setPriority(priority);
		flowTurnConfigDao.updatePriority(product);
	}

	/**审批二次开发停留期为通过*/
	@Override
	public void updateMiddleServiceCycleAuditYes(Integer cycle) throws SQLException {
		flowTurnConfigDao.updateMiddleServiceCycle(cycle);
		flowTurnConfigDao.updateMiddleServiceCycleAuditYes();
	}

	/**审批业绩保留期为拒绝*/
	@Override
	public void updatePerfDayAuditNo() throws SQLException {
		flowTurnConfigDao.updatePerfDayAuditNo();
	}

	/**审批业绩保留期为通过*/
	@Override
	public void updatePerfDayAuditYes() throws SQLException {
		flowTurnConfigDao.updatePerfDayAuditYes();
	}

	/**将产品优先级加入审核*/
	@Override
	public void updatePriorityAddCheck() throws SQLException {
		flowTurnConfigDao.updatePriorityAddCheck();
	}

	/**设置产品优先级为审核通过*/
	@Override
	public void updatePriorityAuditYes() throws SQLException {
		flowTurnConfigDao.updatePriorityAuditYes();
	}

	/**设置产品优先级为审核拒绝*/
	@Override
	public void updatePriorityAuditNo() throws SQLException {
		flowTurnConfigDao.updatePriorityAuditNo();
	}

	/**取得产品优先级审核状态*/
	@Override
	public PublicConfig getPriorityInCheck() throws SQLException {
		return flowTurnConfigDao.getPriorityInCheck();
	}

	/**修改到期预警时间*/
	@Override
	public void updateWarningTime(int time) throws SQLException {
		flowTurnConfigDao.updateWarningTime(time);
	}

	/**获取分配优先级产品列表*/
	@Override
	public List<Product> getAllotPriority() throws SQLException {
		return flowTurnConfigDao.getAllotPriority();
	}

	/**取得默认分配产品*/
	@Override
	public PublicConfig getDefaultProduct() throws SQLException {
		return flowTurnConfigDao.getDefaultProduct();
	}

	/**修改默认分配产品*/
	@Override
	public void updateDefaultProduct(String productId) throws SQLException {
		flowTurnConfigDao.updateDefaultProduct(productId);
	}

	/**修改分配优先级*/
	@Override
	public void updateAllotPriority(String productId,Integer priority) throws SQLException {
		Product product=new Product();
		product.setPriority(priority);
		product.setId(productId);
		flowTurnConfigDao.updateAllotPriority(product);
	}
	
	/**
	 * 获取可用的业绩保留期
	 * @return
	 */
	public PublicConfig getEnablePerfDayInCheck(){
		PublicConfig publicConfig = flowTurnConfigDao.getEnablePerfDayInCheck();
		return publicConfig;
	}

	/**
	 * 取得即将流走客户提醒设置
	 */
	@Override
	public PublicConfig getPublicRemind() throws SQLException {
		return flowTurnConfigDao.getPublicRemind();
	}

	/**
	 * 设置即将流走客户提醒时间
	 */
	@Override
	public PublicConfig updatePublicRemind(Integer remind) throws SQLException {
		return flowTurnConfigDao.updatePublicRemind(remind);
	}
	
}
